package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.JobNodeRunHistory;
import cn.getech.data.development.mapper.JobNodeRunHistoryMapper;
import cn.getech.data.development.model.dto.LogAndIdDto;
import cn.getech.data.development.service.JobDepRunHistoryService;
import cn.getech.data.development.service.JobNodeRunHistoryService;
import cn.getech.data.development.service.JobRunHistoryService;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.Query;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.service.SysUserService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 任务执行历史表 服务实现类
 * </p>
 *
 * @author zenith
 * @since 2018-08-27
 */
@Service
public class JobNodeRunHistoryServiceImpl extends ServiceImpl<JobNodeRunHistoryMapper, JobNodeRunHistory> implements JobNodeRunHistoryService {

    @Autowired
    private JobNodeRunHistoryMapper jobNodeRunHistoryMapper;
    @Autowired
    private JobRunHistoryService jobRunHistoryService;
    @Autowired
    private JobDepRunHistoryService jobDepRunHistoryService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("job_node_info_id");

        IPage<JobNodeRunHistory> page = this.page(
                new Query<JobNodeRunHistory>().getPage(params),
                new QueryWrapper<JobNodeRunHistory>().like(StringUtils.isNotBlank(key),"job_node_info_id", key)
        );

        return new PageUtils(page);
    }

    @Override
    public void removeLogicBatch(List<String> ozJobIds) {
        jobNodeRunHistoryMapper.removeLogicBatch(ozJobIds);
    }

    @Override
    public List<SysUserEntity> logUsers(Integer type) {
        //作业流的创建人
        List<Long> jobUserIds = jobRunHistoryService.logUsers(type);
        //作业流依赖的创建人
        List<Long> jobDepUserIds = jobDepRunHistoryService.logUsers(type);
        Set<Long> lllll = new HashSet<>();
        if(CollectionUtil.isNotEmpty(jobUserIds)){
            lllll.addAll(jobUserIds);
        }
        if(CollectionUtil.isNotEmpty(jobDepUserIds)){
            lllll.addAll(jobDepUserIds);
        }
        List<Long> ids = new ArrayList<>();
        ids.addAll(lllll);
        if(CollectionUtil.isNotEmpty(ids)){
            List<SysUserEntity> users = sysUserService.getUsers(ids);
            if(CollectionUtil.isNotEmpty(users)){
                List<SysUserEntity> rests = new ArrayList<>();
                users.forEach(o -> {
                    SysUserEntity sysUserEntity = new SysUserEntity();
                    sysUserEntity.setUserId(o.getUserId());
                    sysUserEntity.setUsername(o.getUsername());
                    rests.add(sysUserEntity);
                });
                return rests;
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<LogAndIdDto> limitListsByJobId(Integer jobId) {
        return jobNodeRunHistoryMapper.limitListsByJobId(jobId);
    }

    @Override
    public List<String> listByOozieId(String lastOozieJobId) {
        return jobNodeRunHistoryMapper.listByOozieId(lastOozieJobId);
    }

    @Override
    public List<LogAndIdDto> limitListsByOOzieIds(List<String> oozieIds) {
        return jobNodeRunHistoryMapper.limitListsByOOzieIds(oozieIds);
    }
}
